﻿Imports Actuariat.comptes

Public MustInherit Class vueCompte_V2
    Inherits DataGridView
    Implements PTIC_Util.IObserver

    Protected _resultatsScenarioCentral As moteurTauxGaranti2.Moteur_Methode2.resultatsMoteur2
    Protected _ComptesMoteur2 As ComptesMoteur2

    Protected _anneeDepart As Integer
    Protected _nbAnneesProjection As Integer
    Protected _unite As Double
    Protected _nbVirgules As Integer
    Protected _formatDefaut As String

#Region "Propriétés"
    Public Overridable WriteOnly Property moteurTxGaranti2() As moteurTauxGaranti2.Moteur_Methode2
        Set(ByVal value As moteurTauxGaranti2.Moteur_Methode2)
            _ComptesMoteur2 = value.Comptes
            nbAnneesProjection = _ComptesMoteur2.nombreAnneesProjection
            premiereAnneeProj = _ComptesMoteur2.anneePremiereGeneration
            _resultatsScenarioCentral = value.scenarioCentral.resultats
        End Set
    End Property

    Public Property nbAnneesProjection() As Integer
        Get
            Return _nbAnneesProjection
        End Get
        Set(ByVal value As Integer)
            If value >= 1 And value <> _nbAnneesProjection Then
                Me.SuspendLayout()
                _nbAnneesProjection = value
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                ColumnCount = _nbAnneesProjection + 1
                refreshTitresGeneraux()
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                Me.ResumeLayout(False)
            End If
        End Set
    End Property
    Public Property premiereAnneeProj() As Integer
        Get
            Return _anneeDepart
        End Get
        Set(ByVal value As Integer)
            If _anneeDepart <> value Then
                _anneeDepart = value
                refreshTitresGeneraux()
            End If
        End Set
    End Property
    Public Property unite() As Double
        Get
            Return _unite
        End Get
        Set(ByVal value As Double)
            If value <> _unite Then
                _unite = value
                refreshValeursBlocs()
            End If
        End Set
    End Property
    Public Property nbVirgules() As Integer
        Get
            Return _nbVirgules
        End Get
        Set(ByVal value As Integer)
            If _nbVirgules <> value Then
                _nbVirgules = Math.Max(value, 0)
                _formatDefaut = "#,##0"
                If _nbVirgules > 0 Then
                    _formatDefaut = _formatDefaut & "."
                    For i As Integer = 1 To _nbVirgules
                        _formatDefaut = _formatDefaut & "0"
                    Next
                End If
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
                For Each c As DataGridViewColumn In Me.Columns
                    c.DefaultCellStyle.Format = _formatDefaut
                Next
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
                Me.Invalidate()
            End If
        End Set
    End Property
#End Region

#Region "Constructeur"
    Public Sub New()
        parametrageGenerale()
    End Sub
    Public Sub onLoad(ByVal moteur2 As moteurTauxGaranti2.Moteur_Methode2)
        Dim ColumnTitresComptes As New System.Windows.Forms.DataGridViewTextBoxColumn
        CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        ColumnTitresComptes.HeaderText = "Inititulé des comptes"
        ColumnTitresComptes.Name = "ColumnTitresComptes"
        Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {ColumnTitresComptes})
        CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        initNbLignes()
        initLignesTitresGeneraux()
        initTitresBlocs()
        moteurTxGaranti2 = moteur2
    End Sub
    Protected Sub parametrageGenerale()

        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        SetStyle(ControlStyles.ResizeRedraw, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)

        Me.ColumnHeadersVisible = False
        Me.RowHeadersVisible = False
        Me.CellBorderStyle = DataGridViewCellBorderStyle.None
        Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        Me.AllowUserToAddRows = False
        Me.AllowUserToDeleteRows = False
        Me.AllowUserToOrderColumns = False
        Me.AllowUserToResizeColumns = True
        Me.AllowUserToResizeRows = False
        _formatDefaut = "#,##0"
        Me.DefaultCellStyle.Format = _formatDefaut

        Me.nbVirgules = 0

    End Sub
#End Region

#Region "Initialisation"

    Protected MustOverride Sub initNbLignes()
    Protected MustOverride Sub initTitresBlocs()
    Protected MustOverride Sub initLignesTitresGeneraux()

    Protected Function styleCellulesTitre1() As DataGridViewCellStyle
        Dim styleCellsTitre As New DataGridViewCellStyle
        styleCellsTitre.BackColor = Color.FromArgb(88, 29, 116)
        styleCellsTitre.SelectionBackColor = Color.FromArgb(88, 29, 116)
        styleCellsTitre.ForeColor = Color.White
        styleCellsTitre.SelectionForeColor = Color.White
        styleCellsTitre.Font = New System.Drawing.Font("Tahoma", 9, System.Drawing.FontStyle.Bold)
        'styleCellsTitre.Alignment=
        Return styleCellsTitre
    End Function
    Protected Function styleCellulesTitre2() As DataGridViewCellStyle
        Dim styleCellsTitre As New DataGridViewCellStyle
        styleCellsTitre.BackColor = Color.FromArgb(231, 213, 231)
        styleCellsTitre.SelectionBackColor = Color.FromArgb(231, 213, 231)
        styleCellsTitre.ForeColor = Color.Black
        styleCellsTitre.SelectionForeColor = Color.Black
        styleCellsTitre.Font = New System.Drawing.Font("Tahoma", 9, System.Drawing.FontStyle.Bold)
        'styleCellsTitre.Alignment=
        Return styleCellsTitre
    End Function
    Protected Function styleCellulesAccentuees() As DataGridViewCellStyle
        Dim styleCells As New DataGridViewCellStyle
        styleCells.BackColor = Color.FromArgb(255, 230, 30)
        styleCells.SelectionBackColor = Color.FromArgb(255, 230, 30)
        styleCells.ForeColor = Color.Black
        styleCells.SelectionForeColor = Color.Black
        styleCells.Font = New System.Drawing.Font("Tahoma", 9, System.Drawing.FontStyle.Bold)
        Return styleCells
    End Function

#End Region

#Region "Méthodes de dessin"

    Protected MustOverride Sub paintBlocs(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
    Protected MustOverride Function cellFormat(ByVal rowIndex As String, ByVal columnIndex As String) As String
    Protected MustOverride Function cellBackColor(ByVal rowIndex As String, _
                                   ByVal columnIndex As String, _
                                   ByVal ligneHautBloc As Integer, _
                                   ByVal ligneBasBloc As Integer) As System.Drawing.Color
    Protected MustOverride Function cellAlignment(ByVal rowIndex As Integer, ByVal columnIndex As Integer) As StringAlignment
    Protected MustOverride Function cellFont(ByVal rowIndex As Integer, ByVal columnIndex As Integer, ByVal ligneHautBloc As Integer, ByVal ligneBasBloc As Integer) As Font

    Protected Overrides Sub OnCellPainting(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        'Colonne de titre
        paintLigneTitre(e, 0)
        'Les blocs
        paintBlocs(e)
    End Sub
    Protected Sub paintLigneTitre(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs, _
                                  ByVal numLigne As Integer)
        Dim g As Graphics = e.Graphics
        Dim cellBounds As System.Drawing.Rectangle = e.CellBounds
        Dim rowIndex As Integer = e.RowIndex, columnIndex As Integer = e.ColumnIndex

        If rowIndex = numLigne Then

            Dim cellEnCoursValue As Object = Me.Rows(rowIndex).Cells(columnIndex).Value
            Dim cellEnCoursBackColor As System.Drawing.Color = Color.FromArgb(88, 29, 116)
            Dim cellEnCoursFormat As String = ""
            Dim cellEnCoursFont As Font = New System.Drawing.Font("Tahoma", 9, FontStyle.Bold)
            Dim cellEnCoursAlignement As StringAlignment = IIf(columnIndex = 0, StringAlignment.Near, StringAlignment.Far)

            Dim backColorBrush As New SolidBrush(cellEnCoursBackColor)
            Dim gridBrush As New SolidBrush(Color.Black)
            Dim gridLinePen As New Pen(gridBrush)

            Try
                'Tracé du quadrillage
                g.FillRectangle(backColorBrush, cellBounds)
                'Remplissage des valeurs
                If (e.Value IsNot Nothing) Then
                    Dim rectText As Rectangle
                    With ColumnHeadersDefaultCellStyle.Padding
                        rectText = New Rectangle(cellBounds.X + .Left, _
                                                 cellBounds.Y + .Top, _
                                                 cellBounds.Width - .Left - .Right - 2, _
                                                 cellBounds.Height - .Top - .Bottom)
                    End With

                    Dim stringFormat As New StringFormat()
                    stringFormat.Alignment = cellEnCoursAlignement
                    stringFormat.LineAlignment = StringAlignment.Center
                    g.DrawString(Format(cellEnCoursValue, cellEnCoursFormat), _
                                 cellEnCoursFont, _
                                 New SolidBrush(Color.White), _
                                 rectText, _
                                 stringFormat)
                End If
                e.Handled = True
            Finally
                gridLinePen.Dispose()
                gridBrush.Dispose()
                backColorBrush.Dispose()
            End Try
        End If
    End Sub
    Protected Sub paintBloc(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs, _
                      ByVal ligneHautBloc As Integer, _
                      ByVal ligneBasBloc As Integer)

        Dim g As Graphics = e.Graphics
        Dim cellBounds As System.Drawing.Rectangle = e.CellBounds
        Dim rowIndex As Integer = e.RowIndex, columnIndex As Integer = e.ColumnIndex

        If (rowIndex >= ligneHautBloc And rowIndex <= ligneBasBloc) And (columnIndex <= _nbAnneesProjection) Then

            Dim cellEnCoursValue As Object = Me.Rows(rowIndex).Cells(columnIndex).Value
            Dim cellEnCoursBackColor As System.Drawing.Color = cellBackColor(rowIndex, columnIndex, ligneHautBloc, ligneBasBloc)
            Dim cellEnCoursFormat As String = cellFormat(e.RowIndex, e.ColumnIndex)
            Dim cellEnCoursFont As Font = cellFont(rowIndex, columnIndex, ligneHautBloc, ligneBasBloc)
            Dim cellEnCoursAlignement As StringAlignment = cellAlignment(rowIndex, columnIndex)

            Dim backColorBrush As New SolidBrush(cellEnCoursBackColor)
            Dim gridBrush As New SolidBrush(Color.Black)
            Dim gridLinePen As New Pen(gridBrush)

            Try
                'Tracé du quadrillage
                g.FillRectangle(backColorBrush, cellBounds)
                If rowIndex = ligneHautBloc + 1 Or rowIndex = ligneBasBloc Then
                    'Bordure basse
                    g.DrawLine(gridLinePen, cellBounds.Left, _
                        cellBounds.Bottom - 1, cellBounds.Right - 1, _
                        cellBounds.Bottom - 1)
                End If
                If rowIndex > ligneHautBloc + 1 Then
                    'Bordure Droite
                    g.DrawLine(gridLinePen, cellBounds.Right - 1, _
                        cellBounds.Top, cellBounds.Right - 1, _
                        cellBounds.Bottom - 1)
                End If
                If e.ColumnIndex = 0 And e.RowIndex > ligneHautBloc + 1 Then
                    'Bordure gauche
                    g.DrawLine(gridLinePen, cellBounds.Left, _
                        cellBounds.Top, cellBounds.Left, _
                        cellBounds.Bottom - 1)
                End If

                'Remplissage des valeurs
                If (e.Value IsNot Nothing) Then
                    Dim rectText As Rectangle
                    With ColumnHeadersDefaultCellStyle.Padding
                        rectText = New Rectangle(cellBounds.X + .Left, _
                                                 cellBounds.Y + .Top, _
                                                 cellBounds.Width - .Left - .Right - 2, _
                                                 cellBounds.Height - .Top - .Bottom)
                    End With

                    Dim stringFormat As New StringFormat()
                    stringFormat.Alignment = cellEnCoursAlignement
                    stringFormat.LineAlignment = StringAlignment.Center
                    g.DrawString(Format(cellEnCoursValue, cellEnCoursFormat), _
                                 cellEnCoursFont, _
                                 New SolidBrush(Me.DefaultCellStyle.ForeColor), _
                                 rectText, _
                                 stringFormat)
                End If
                e.Handled = True
            Finally
                gridLinePen.Dispose()
                gridBrush.Dispose()
                backColorBrush.Dispose()
            End Try
        End If
    End Sub

#End Region

#Region "Redirection des rafraichissements invoqués par les objets métiers"
    Public Overridable Function actualise(ByVal sender As Object, ByVal notification As Object) As PTIC_Util.resultatValidationContraintes Implements PTIC_Util.IObserver.actualise
        'Actualisation des primes
        If sender Is _ComptesMoteur2 Then
            'Actualiser depuis un changement de structure des comptes restitués
            actualiseFromParamCompteMoteur2(sender, notification)
        End If
        Return Nothing
    End Function
    Private Sub actualiseFromParamCompteMoteur2(ByVal comptesRestitues As ComptesMoteur2, _
                                            ByVal notification As ComptesMoteur2.typeModification)
        If notification = ComptesMoteur2.typeModification.nbAnneesProjection Then
            nbAnneesProjection = comptesRestitues.nombreAnneesProjection
        ElseIf notification = ComptesMoteur2.typeModification.premiereAnneeProjection Then
            premiereAnneeProj = comptesRestitues.anneePremiereGeneration
        End If
    End Sub
#End Region

#Region "Rafraichissements élémentaires"
    Protected MustOverride Sub refreshTitresGeneraux()
    Protected MustOverride Sub refreshValeursBlocs()
#End Region


    <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
    Private components As System.ComponentModel.IContainer

End Class